package com.wind.trademark.common.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;


@Component
public class ExceptionResolver extends AbstractHandlerExceptionResolver {

	final Logger log = LoggerFactory.getLogger(ExceptionResolver.class);


	
	@Override
	protected ModelAndView doResolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {

		logger.error("Error:", ex);

		if (ex instanceof TmException) {

			// this is special type of exception, do not show error but redirect
			// to other page
			String errorCode= ((TmException) ex).getErrorCode();
			ModelAndView view = new ModelAndView("jsonView");
			view.addObject("error",true);
			view.addObject("errorCode",errorCode );
			view.addObject("errorMsg",ex.getMessage());
			//response.setStatus(WebConstants.ERROR_CODE);
			return view;

		} else {
			// show error page with short explanation
			ModelAndView view = new ModelAndView("jsonView");
			view.addObject("error",true);
			view.addObject("errorCode", TmException.STATUS_EXCEPTION);
			view.addObject("errorMsg",ex.getMessage());
			//response.setStatus(WebConstants.ERROR_CODE);
			return view;
		}
	}
}